% this reads in the ADV log files, saves the data in a .MAT format 

function datastr = convertADVlog_profile2MAT_Sontek_deployed(filename)
  

[r,c]=size(filename)

for k=1:r
    
    fname = filename(k,:)

fid=fopen([fname '.log']);
count = 1;
while ~feof(fid)

t2=fgetl(fid);

val=str2num(t2);


% test for same number of values on each line
if length(val)==15 
    data(count,:)=val;
    count = count+1;
end


end


end

recordno=data(:,1);
Vx=data(:,2)/100;   %velocity in cm/sec
Vy=data(:,3)/100;
Vz=data(:,4)/100;
A1=data(:,5);       %signal amplitude in counts  
A2=data(:,6);
A3=data(:,7);
C1=data(:,8);       %correlation score expressed as a percentage
C2=data(:,9);
C3=data(:,10);
heading=data(:,11)/10;       % degree  
pitch=data(:,12)/10;
roll=data(:,13)/10;
ADVtemp=data(:,14)/100;    % degree C
ADVpress=data(:,15);    % units of counts ranging from 0 to 65535

eval (['save ' fname '  Vx Vy Vz A1 A2 A3 C1 C2 C3 heading pitch roll ADVtemp ADVpress '])


figure (100)
plot (-data(:,15))
[x y]=ginput
x=round(x)


proVx=data(x(1):x(2),2)/100;   
proVy=data(x(1):x(2),3)/100;
proVz=data(x(1):x(2),4)/100;
proA1=data(x(1):x(2),5);      
proA2=data(x(1):x(2),6);
proA3=data(x(1):x(2),7);
proC1=data(x(1):x(2),8);       
proC2=data(x(1):x(2),9);
proC3=data(x(1):x(2),10);
pro_heading=data(x(1):x(2),11)/10;       
pro_pitch=data(x(1):x(2),12)/10;
pro_roll=data(x(1):x(2),13)/10;
pro_ADVtemp=data(x(1):x(2),14)/100;   
pro_ADVpress=data(x(1):x(2),15);



eval (['save ' fname '-pro  proVx proVy proVz proA1 proA2 proA3 proC1 proC2 proC3 pro_heading pro_pitch pro_roll pro_ADVtemp pro_ADVpress '])

eval (['save ' fname 'index.asc x -ascii'])  %1st 2 are profile start and stop times and rest are TSS depth start and stop times


    
fclose(fid);

clear Vx Vy Vz A1 A2 A3 C1 C2 C3 heading pitch roll temp pressure
clear proVx proVy proVz proA1 proA2 proA3 proC1 proC2 proC3 pro_heading pro_pitch pro_roll pro_ADVtemp pro_ADVpress
clear x
end
